package com.example.mybatis.mapper;

import com.example.mybatis.model.UserInfo;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    //@ResultMap("resultMap")
    @Select("select * from userinfo")
    List<UserInfo> getUserInfoAll();

    @Select("select * from userinfo where id= #{id}")
    List<UserInfo> getInfo(Integer id);

    @Select("select * from userinfo where gender = #{gender}")
    List<UserInfo> getInfo2(Integer gender);

    // 增
    @Insert("insert into userinfo (username, password, age, gender) values (#{username},#{password}, #{age}, #{gender})")
    Integer insert(UserInfo userInfo);

    // 删
    @Delete("delete from userinfo where id = #{id}")
    Integer delete(Integer id);

    // 改
    @Update("update userinfo set password = #{password}, age = #{age} where id = #{id}")
    Integer update(UserInfo userInfo);

    //查询
    @Select("select * from userinfo")
    List<UserInfo> getUserInfo();

    //起别名
    @Select("select id, username, password, age, gender, phone, delete_flag as deleteFlag, " +
            "create_time as createTime, update_time as updateTime from userinfo")
    public List<UserInfo> queryUser();

    //结果映射
    @Results(id = "resultMap", value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("select * from userinfo")
    List<UserInfo> queryUser2();

    @Select("select * from userinfo")
    List<UserInfo> queryUser3();

    //#{} 和 ${}的使用
    @Select("select id, username, password, age, gender, phone from userinfo where id = #{id}")
    List<UserInfo> queryById(Integer id);
    @Select("select id, username, password, age, gender, phone from userinfo where id = ${id}")
    List<UserInfo> queryById2(Integer id);
    @Select("select id, username, password from userinfo where name = ${username}")
    List<UserInfo> queryByName(String name);

//    排序
    @Select("select id, username, password from userinfo order by id ${sort}")
    List<UserInfo> queryBySort(String sort);

    //模糊匹配
    @Select("select id, username, password from userinfo where username like concat('%', #{key}, '%')")
    List<UserInfo> queryByLike(String key);



}
